package com.lovo.controller;

import com.lovo.dos.MoiveDO;
import com.lovo.dto.BuyDTO;
import com.lovo.dto.SellDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/30
 */
@RestController
public class SellController {
    private static Map<Integer, MoiveDO> map = new HashMap<>();
    static {
        map.put(1,new MoiveDO(1,20,53));
        map.put(2,new MoiveDO(2,12,32));
        map.put(3,new MoiveDO(3,22,13));
        map.put(4,new MoiveDO(4,1,30));
    }

    @PostMapping("sell")
    public SellDTO sell(@RequestBody BuyDTO buyDTO){
        Integer id = buyDTO.getId();
        Integer num = buyDTO.getNum();
        MoiveDO moiveDO = map.get(id);
        if (moiveDO == null) return new SellDTO("没有这个电影","失败");
        if (moiveDO.getNum() < num){
            return new SellDTO("电影票不足","失败");
        }
        moiveDO.setNum(moiveDO.getNum() - num);
        UUID uuid = UUID.randomUUID();
        return new SellDTO("取票编码:"+uuid+"" +
                ",   总价格为"+moiveDO.getPrice()*num,"成功");
    }

}
